from PyQt5.QtWidgets import (QMessageBox, QFileDialog, QAbstractItemView)
from PyQt5.QtCore import Qt

class ImageSelector:
    @staticmethod
    def select_images(parent=None):
        """
        选择图片文件或文件夹
        返回: (selected_files, is_folder)
              selected_files: 选择的文件列表或文件夹路径
              is_folder: 是否是文件夹选择
        """
        # 创建选择对话框
        dialog = QMessageBox(parent)
        dialog.setWindowTitle("选择导入方式")
        dialog.setText("请选择导入方式:")
        
        # 添加按钮
        btn_files = dialog.addButton("选择图片文件", QMessageBox.ActionRole)
        btn_folder = dialog.addButton("选择图片文件夹", QMessageBox.ActionRole)
        dialog.addButton(QMessageBox.Cancel)
        
        dialog.exec_()
        
        if dialog.clickedButton() == btn_files:
            return ImageSelector._select_image_files(parent), False
        elif dialog.clickedButton() == btn_folder:
            return ImageSelector._select_image_folder(parent), True
        else:
            return None, False
    
    @staticmethod
    def _select_image_files(parent=None):
        """选择多个图片文件"""
        file_dialog = QFileDialog(parent)
        file_dialog.setFileMode(QFileDialog.ExistingFiles)
        file_dialog.setNameFilter("图片文件 (*.jpg *.jpeg *.png *.bmp *.gif)")
        
        # 设置多选模式
        file_view = file_dialog.findChild(QAbstractItemView, "listView")
        if file_view:
            file_view.setSelectionMode(QAbstractItemView.ExtendedSelection)
        tree_view = file_dialog.findChild(QAbstractItemView, "treeView")
        if tree_view:
            tree_view.setSelectionMode(QAbstractItemView.ExtendedSelection)
        
        if file_dialog.exec_():
            return file_dialog.selectedFiles()
        return None
    
    @staticmethod
    def _select_image_folder(parent=None):
        """选择图片文件夹"""
        folder_path = QFileDialog.getExistingDirectory(parent, "选择图片文件夹")
        return folder_path if folder_path else None